//Z 字形变换
//测试链接 https://leetcode.cn/problems/zigzag-conversion/description/
public class Convert {
    public String convert(String s, int numRows) {
        if(numRows == 1){
            return s;
        }
        int len = s.length();
        StringBuilder sb = new StringBuilder();

        int d = 2 * numRows - 2; //计算公差
        for(int i = 0; i < numRows; i++){
            if(i == 0){
                int index = i;
                while(index < len){
                    sb.append(""+s.charAt(index));
                    index+=d;
                }

            }else if(i == numRows - 1){
                int index = i;
                while(index < len){
                    sb.append(""+s.charAt(index));
                    index+=d;
                }

            }else {
                int left = i, right = d - i;
                while(left < len || right < len){
                    if(left < len){
                        sb.append(""+s.charAt(left));
                    }
                    if(right < len){
                        sb.append(""+s.charAt(right));
                    }

                    left += d;
                    right += d;
                }
            }
        }

        return sb.toString();
    }
}
